//
//  DynamicCell.m
//  Qianbancaifu
//
//  Created by SYLH on 16/4/26.
//  Copyright © 2016年 SYLH. All rights reserved.
//

#import "DynamicCell.h"


@interface DynamicCell ()<DynamicContentDelegate>

@property (nonatomic,strong) UIImageView * headImage;       //动态发布者头像

@property (nonatomic,strong) UIImageView * vipImageView;

@property (nonatomic,strong) UILabel * nameLabel;           //动态发布者名字

@property (nonatomic,strong) UIView * lineView;             //分割线

@property (nonatomic,strong) DynamicRedCountView * readCountView;  //阅读量

@property (nonatomic,strong) UIButton * accusationButton;           //动态举报按钮

@property (nonatomic,strong) UILabel * reportDetailLabel;           //举报内容

@property (nonatomic,strong) UILabel * timeLabel;                   //动态发布时间

@property (nonatomic,strong) UIButton * likeButton;                 //转发按钮

@property (nonatomic,strong) UILabel * likeLabel;                   //评论数

@property (nonatomic,strong) DynamicContentView * dynamicContentView;     //动态内容

@property (nonatomic,strong) DynamicModel * dynamicModel;    //动态Model

@property (nonatomic,assign) BOOL showDelete;                   //删除

@property (nonatomic,assign) BOOL showReportCount;              //显示被举报次数



@end

@implementation DynamicCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        self.lineView = [[UIView alloc] init];
        self.lineView.backgroundColor = LINE_COLOR;
        [self.contentView addSubview:self.lineView];
        
        //动态发布者头像
        self.headImage = [[UIImageView alloc] init];
        self.headImage.frame = CGRectMake(12, 10, 50, 50);
        self.headImage.contentMode = UIViewContentModeScaleAspectFill;
        self.headImage.clipsToBounds = YES;
        self.headImage.tag = ContactBtn_tag;
        self.headImage.userInteractionEnabled = YES;
        self.headImage.exclusiveTouch = YES;
        [self.contentView addSubview:self.headImage];
        
        //点击手势识别器
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processGesture:)];
        [self.headImage addGestureRecognizer:tap];
        
        self.vipImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.vipImageView];
        
        //动态发布者名字
        self.nameLabel = [[UILabel alloc] init];
        self.nameLabel.textColor = [UIColor colorWithHex:0x080808 alpha:1.0];
        self.nameLabel.font = PFM_FONT(14);
        [self.contentView addSubview:self.nameLabel];
        
        //阅读量
        self.readCountView = [[DynamicRedCountView alloc] init];
        [self.contentView addSubview:self.readCountView];
        
        //动态发布时间
        self.timeLabel = [[UILabel alloc] init];
        self.timeLabel.font = PFR_FONT(12);
        self.timeLabel.textColor = [UIColor colorWithHex:0x959595 alpha:1.0];
        [self.contentView addSubview:self.timeLabel];
        
        //举报
        [self.contentView addSubview:self.accusationButton];
        
        
        //赞按钮
        self.likeButton = [[UIButton alloc] init];
        self.likeButton.exclusiveTouch = YES; //不允许同时多个按钮点击
        [self.likeButton setImage:[UIImage imageNamed:@"com_unLike"] forState:UIControlStateNormal];
        [self.likeButton setImage:[UIImage imageNamed:@"com_like"] forState:UIControlStateSelected];
        self.likeButton.tag = LikeBtn_tag;
        [self.likeButton addTarget:self action:@selector(clickBottomBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.likeButton];
        
        //赞数量
        self.likeLabel = [[UILabel alloc] init];
        self.likeLabel.textColor = [UIColor colorWithHex:0xa4a4a4 alpha:1.0];
        self.likeLabel.font = PFR_FONT(14);
        [self.contentView addSubview:self.likeLabel];
        
        self.dynamicContentView = [[DynamicContentView alloc] init];
        self.dynamicContentView.delegate = self;
        [self.contentView addSubview:self.dynamicContentView];
        
        
        self.reportDetailLabel = [[UILabel alloc] init];
        self.reportDetailLabel.textColor = [UIColor redColor];
        self.reportDetailLabel.font = PFR_FONT(12);
        [self.contentView addSubview:self.reportDetailLabel];
        
    }
    return self;
}

#pragma mark - get
-(UIButton *)accusationButton
{
    if (!_accusationButton) {
        _accusationButton = [[UIButton alloc] init];
        _accusationButton.exclusiveTouch = YES;
        [_accusationButton setTitle:KNSLocal(@"reportTitle") forState:UIControlStateNormal];
        _accusationButton.titleLabel.font = PFR_FONT(12);
        [_accusationButton setTitleColor:[UIColor colorWithHex:0x6e7b9e alpha:1.0] forState:UIControlStateNormal];
        [_accusationButton setTitleColor:[UIColor colorWithHex:0xcccccc alpha:1.0] forState:UIControlStateHighlighted];
        _accusationButton.tag = ReportBtn_tag;
        [_accusationButton addTarget:self action:@selector(clickBottomBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _accusationButton;
}

-(void)creatCellWithModel:(DynamicModel *)model
               showDelete:(BOOL)showDelete
          showReportCount:(BOOL)showReportCount
{
    _dynamicModel = model;
    self.showDelete = showDelete;
    self.showReportCount = showReportCount;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //分割线
        self.lineView.frame = CGRectMake(0, _dynamicModel.totalHeight - 1 , KScreen_Width,1);

        //动态发布者头像
        [self.headImage sd_setImageWithURL:LOADINGTHUMBIMG_FIXED_URL(_dynamicModel.contact.headImage)
                                       placeholderImage:[UIImage imageNamed:@"placeholder"]
                                                options:SDWebImageRetryFailed
                                              completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                                   [[SDImageCache sharedImageCache] clearDisk];
                                              }];
        //动态发布者名字
        self.nameLabel.frame = CGRectMake(CGRectGetMaxX(self.headImage.frame) + 10,16, 200, 16);
        self.nameLabel.text = _dynamicModel.contact.nickName;
        self.nameLabel.numberOfLines = 1;
        [self.nameLabel sizeToFit];
        
        //vip等级
        if (_dynamicModel.contact.userVipLevel > 0) {
            self.vipImageView.hidden = NO;
            self.vipImageView.frame = CGRectMake(CGRectGetMaxX(self.nameLabel.frame) + 4, 16, 22, 22);
            self.vipImageView.image = _dynamicModel.contact.userVipLevelImage;
        }else{
            self.vipImageView.hidden = YES;
        }
        
    
        //阅读量
        self.readCountView.redCount = _dynamicModel.customReadTotalNum;
        
        //动态发布时间
        self.timeLabel.frame = CGRectMake(CGRectGetMaxY(self.headImage.frame) + 10,_dynamicModel.totalHeight - 24, 200, 16);
        self.timeLabel.text = _dynamicModel.formatTime1;
        self.timeLabel.numberOfLines = 1;
        [self.timeLabel sizeToFit];

        //举报按钮
        self.accusationButton.frame = CGRectMake(CGRectGetMaxX(self.timeLabel.frame) + 4, CGRectGetMinY(self.timeLabel.frame), 40, 16);
        self.reportDetailLabel.hidden = YES;
        if (self.showDelete &&
            ([_dynamicModel.contact.objectId isEqualToString:[UserInfoManger shared].contact.objectId] ||
             [UserInfoManger shared].userIsAdmin)) {
                [self.accusationButton setTitle:KNSLocal(@"deleteTitle") forState:UIControlStateNormal];
                [self.accusationButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                
                if ([UserInfoManger shared].userIsAdmin && self.showReportCount) {
                    self.reportDetailLabel.hidden = NO;
                    self.reportDetailLabel.frame = CGRectMake(CGRectGetMaxX(self.accusationButton.frame) + 5, CGRectGetMinY(self.accusationButton.frame), 150, 16);
                    self.reportDetailLabel.text = [NSString stringWithFormat:@"被举报：%ld次",_dynamicModel.reportList.count];
                }
        }else{
            [self.accusationButton setTitle:KNSLocal(@"reportTitle") forState:UIControlStateNormal];
            [self.accusationButton setTitleColor:[UIColor colorWithHex:0x6e7b9e alpha:1.0] forState:UIControlStateNormal];
        }
        
        //赞内容
        NSDictionary * attriDic = @{NSFontAttributeName:self.likeLabel.font};
        CGSize likeSize = [_dynamicModel.formatLikeTotalNum boundingRectWithSize:CGSizeMake(100, MAXFLOAT)
                                                                   options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                                                attributes:attriDic
                                                                   context:nil].size;
        CGFloat  likeWidth = likeSize.width + 10; //加个10 留点空间，防止数字改变，文字无法显示完全
        
        self.likeLabel.frame = CGRectMake(KScreen_Width - likeWidth - 12, _dynamicModel.totalHeight - 24, likeWidth, 16);
        self.likeLabel.text = _dynamicModel.formatLikeTotalNum;
        
        CGFloat imageWith = 25 + 4; //图片宽度25 + 4图片和文字间距
        //赞按钮
        self.likeButton.frame = CGRectMake(CGRectGetMinX(self.likeLabel.frame) - imageWith, _dynamicModel.totalHeight - 27, imageWith + likeWidth, 22);
        CGFloat edgeWidth = (likeWidth + 4 ) / 2; //4 图片和文字间距
        self.likeButton.imageEdgeInsets = UIEdgeInsetsMake(0, -edgeWidth, 0, edgeWidth);
        self.likeButton.layer.anchorPoint = CGPointMake(10.0 / (likeWidth + imageWith), 0.5); 
        self.likeButton.frame = CGRectMake(CGRectGetMinX(self.likeLabel.frame) - imageWith, _dynamicModel.totalHeight - 27, imageWith + likeWidth, 22);
        
        self.likeButton.selected = _dynamicModel.isLike;
        if (_dynamicModel.isLike) {
            self.likeLabel.textColor = [UIColor colorWithHex:0xff6c6c alpha:1.0];
        }else{
            self.likeLabel.textColor = [UIColor colorWithHex:0xa4a4a4 alpha:1.0];
        }
    
        /**
         *动态内容区域
         */
        self.dynamicContentView.frame = CGRectMake(CGRectGetMaxY(self.headImage.frame) + 10,48, _dynamicModel.contentWidth, _dynamicModel.contentHeight);
        [self.dynamicContentView creatDynamicContentViewWithModel:model];
        
    });

}


#pragma mark - 点击评论，收藏，点赞，转发按钮
-(void)clickBottomBtn:(UIButton *)sender
{
    switch (sender.tag) {
        case LikeBtn_tag:    //点赞
        {
            
        }
            break;
        case ReportBtn_tag:    //举报
        {
            
        }
            break;
        case ContactBtn_tag:
        {
            
        }
            break;
        default:
            break;
    }
    if ([self.delegate respondsToSelector:@selector(likeDynamicWithSender:clickCell:)]) {
        [self.delegate likeDynamicWithSender:sender clickCell:self];
    }
}

#pragma mark - 手势
- (void) processGesture:(UISwipeGestureRecognizer *)gesture
{
    //点击手势
    if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) {
        
        if ([self.delegate respondsToSelector:@selector(likeDynamicWithSender:clickCell:)]) {
            [self.delegate likeDynamicWithSender:gesture.view clickCell:self];
        }
        
    }
}

#pragma mark - 放大缩小动画
-(void)likeAnimationWithSender:(UIButton*)sender
{
    if (sender.selected) {
        [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            sender.transform = CGAffineTransformMakeScale(1.5, 1.5);
        } completion:^(BOOL finished) {
            
            [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                sender.transform = CGAffineTransformMakeScale(1, 1);
            } completion:^(BOOL finished) {
                
            }];
        }];
    }
}

#pragma mark - 点赞数改变
-(void)changeLikeNumWithDynamic:(DynamicModel*)model
{

    self.likeButton.selected = model.isLike;
    
    [self likeAnimationWithSender:self.likeButton];
    //改变数量
    self.likeLabel.text = model.formatLikeTotalNum;
    if (model.isLike) {
        self.likeLabel.textColor = [UIColor colorWithHex:0xff6c6c alpha:1.0];
    }else{
        self.likeLabel.textColor = [UIColor colorWithHex:0xa4a4a4 alpha:1.0];
    }
}

#pragma mark - 删除动态按钮
-(void)processDeleleButton
{
    if ([self.delegate respondsToSelector:@selector(deleteDynamicWithCell:)]) {
        [self.delegate deleteDynamicWithCell:self];
    }
}


#pragma mark - contentView deleagte
#pragma mark - 显示更多
-(void)isShowMoreContent:(BOOL)showMore sender:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(isShowMoreContent:withCell:sender:)]) {
        [self.delegate isShowMoreContent:showMore withCell:self sender:sender];
    }
}
#pragma mark - 点击文字中的链接
-(void)processCommunityUrlLink:(NSString *)link
{
    if ([self.delegate respondsToSelector:@selector(processCommunityUrlLink:cell:)]) {
        [self.delegate processCommunityUrlLink:link cell:self];
    }
}

@end
